package com.imooc.reader.controller;

import com.imooc.reader.exception.BussiException;
import com.imooc.reader.entity.Evaluation;
import com.imooc.reader.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class EvaluationController {

    @Autowired
    private EvaluationService evaluationService;

    /**
     * 新增短评
     * @param bookId
     * @param memberId
     * @param score
     * @param content
     * @return
     */
    @RequestMapping("/insertComment")
    @ResponseBody
    public Map insertComment(Long bookId,Long memberId,Integer score,String content){
        Map map=new HashMap();
        try {
            evaluationService.insertComment(bookId,memberId,score,content);
            map.put("code","0");
            map.put("msg","success");
        }catch (BussiException ex){
            ex.printStackTrace();
            map.put("code",ex.getCode());
            map.put("msg",ex.getMsg());
        }
        return map;
    }

    /**
     * 更新点赞
     * @param evaluationId
     * @return
     */
    @RequestMapping("/updateEnjoy")
    @ResponseBody
    public Map updateEnjoy(Long evaluationId){
        Map map=new HashMap();
        try {
            Evaluation evaluation = evaluationService.updateEnjoy(evaluationId);
            map.put("code","0");
            map.put("msg","success");
            map.put("evaluation",evaluation);
        }catch (BussiException ex){
            ex.printStackTrace();
            map.put("code",ex.getCode());
            map.put("msg",ex.getMsg());
        }
        return map;
    }
}
